package tools.maf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class mafUtils {
	private static HashMap<String, String> methodsHelp= new HashMap<String, String>();
//	private final static String sep="\t";
//	private final static String[] nucleotides= new String[]{"A","C","G","T","N"};
	
	
	
	public static void main(String[] args)throws Exception{
//		methodsHelp.put("test", "test - test the parser\n\targs = <mafAlignment>");
		methodsHelp.put("trimOverlapping", "trimOverlapping - trims the overlapping region of paired alignments. Assumes that two paired sequences appear after each other and has the same name\n\targs = <mafAlignment>");
		
		if(args.length>0){
			if(args[0].equals("test")&&args.length==2){
				test(args[1]);
			}else if(args[0].equals("trimOverlapping")&&args.length==2){
				trimOverlapping(args[1]);
			}else{
				System.err.println(printHelp(args[0]));
			}
		}else{
			System.err.println(printHelp());
		}
	}
	
	private static void trimOverlapping(String inFile)throws Exception{
		mafParser mp= new mafParser(inFile);
		mafAlignment cur, prev;
		boolean keepPrev,useThis;
		int overlap,trimA,trimB;
		if(mp.hasNext()){
			prev=mp.next();
			keepPrev=true;
			for(;mp.hasNext();){
				cur=mp.next();
				useThis=true;
				if(cur.getQname().equals(prev.getQname())){
					//check to see if trimming should be done
					if(cur.overlapsNoStrand(prev)){
						if(cur.getTstart()<prev.getTstart()){
							overlap=cur.getTend()-prev.getTstart();
							trimA=overlap/2+overlap%2;
							trimB=overlap/2;
							if(trimA>=cur.getAlignmentLength()){
								useThis=false;
							}else if(trimB>=prev.getAlignmentLength()){
								keepPrev=false;
							}else{
								cur.trimTargetRight(trimA);
								prev.trimTargetLeft(trimB);
							}
						}else{
							overlap=prev.getTend()-cur.getTstart();
							trimA=overlap/2+overlap%2;
							trimB=overlap/2;
							if(trimA>=prev.getAlignmentLength()){
								useThis=false;
							}else if(trimB>=cur.getAlignmentLength()){
								keepPrev=false;
							}else{
								prev.trimTargetRight(trimA);
								cur.trimTargetLeft(trimB);
							}
						}
						if(useThis && keepPrev && cur.overlapsNoStrand(prev)){
							System.err.println("failed trimming: \n"+prev.toString()+"\nand\n"+cur.toString());
						}
						
					}
				}
				if(keepPrev){
					System.out.println(prev.toString());
				}
				if(useThis){
					prev=new mafAlignment(cur);
					keepPrev=true;
				}
			}
			if(keepPrev){
				System.out.println(prev.toString());
			}
		}
	}
	
	private static void test(String inFile) throws Exception{
		mafParser mp= new mafParser(inFile);
		mafAlignment ma= new mafAlignment();
		
		for(;mp.hasNext();){
			ma=mp.next();
			System.out.println(ma.toString());
		}
		
		mp.close();
		
//		for(String s=in.readLine();s!=null;s=in.readLine()){
//			if(s.length()>0 && s.charAt(0)=='a'){
//				ma= new mafAlignment(s);
//				s=in.readLine();
//				ma.addTargetSline(s);
//				s=in.readLine();
//				ma.addQuerySline(s);
//				s=in.readLine();
//				ma.addQueryQline(s);
//				
//				System.out.println(ma.toString());
//			}
//		}
		
//		mafSequence ms=new mafSequence();
//		
//		for(String s=in.readLine();s!=null;s=in.readLine()){
//			if(s.length()>0){
//				if(s.charAt(0)=='s'){
//					ms= new mafSequence(s);
//				}
//				if(s.charAt(0)=='q'){
//					ms.addQualityLine(s);
//				}
//			}else{
//				System.out.println(ms);
//			}
//		}
	}
	
	private static String printHelp(){
		String help="";
		ArrayList<String> cmds= new ArrayList<String>(methodsHelp.keySet());
		Collections.sort(cmds);
		for (String s : cmds) {
			help+=methodsHelp.get(s);
		}
		
		return help;
	}
	
	private static String printHelp(String method){
		if(methodsHelp.containsKey(method)){
			return methodsHelp.get(method);
		}
		return printHelp();
	}
}
